package com.diy.sigmund.program;

import com.diy.sigmund.AsyncTransactionalApplication;
import com.diy.sigmund.entity.Employee;
import com.diy.sigmund.mapper.EmployeeMapper;
import com.diy.sigmund.service.IEmployeeService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 问题：does not support filtering and will therefore be run completely.
 * <br>解决方案：将引入的org.junit.jupiter.api.Test;这个包替换成org.junit.Test;
 */
//@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ThreadTest01.class, AsyncTransactionalApplication.class})
public class ThreadTest01 {

    @Resource
    private IEmployeeService employeeBO;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 测试多线程事务.
     */
    @Test
    public void moreThreadTest2() {
        int size = 10;
        Random random = new Random();
        List<Employee> employeeList = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Employee employee = new Employee();
            employee.setId(Math.abs(random.nextLong()));
            employee.setEmployeeName("lol" + i);
            employee.setAge(18);
            employee.setGender(1);
            employee.setIdNumber(i + "XX");
            employee.setCreateTime(LocalDateTime.now());
            employeeList.add(employee);
        }
        try {
            // employeeMapper.saveBatch(employeeList);
            // employeeBO.saveThreadException(employeeList);
            employeeBO.saveThread(employeeList);
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Random random = new Random();
        System.out.println(Math.abs(random.nextLong()));
    }
}
